﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Utility.DBUtility.DatabaseOperate
{
    /// <summary>
    /// ORM 数据库操作对象集合
    /// </summary>
    public static class ORMCommData
    {
        /// <summary>
        /// 数据库操作对象集合[线程安全]
        /// </summary>
        public static ConcurrentDictionary<string, AdoNetObject> AdoNetObjects = new ConcurrentDictionary<string, AdoNetObject>();
    }
    /// <summary>
    /// 数据库操作对象
    /// 如果新增数据库,需要添加对应点 ADO.NET 对象
    /// </summary>
    public class AdoNetObject
    {
        public Oracle.ManagedDataAccess.Client.OracleConnection OracleConnection
        { get; set; }
        public MySql.Data.MySqlClient.MySqlConnection MySqlConnection
        { get; set; }
        public System.Data.SqlClient.SqlConnection SqlConnection
        { get; set; }
        public System.Data.SQLite.SQLiteConnection SQLiteConnection
        { get; set; }

        public Oracle.ManagedDataAccess.Client.OracleTransaction OracleTransaction
        { get; set; }
        public MySql.Data.MySqlClient.MySqlTransaction MySqlTransaction
        { get; set; }
        public System.Data.SqlClient.SqlTransaction SqlTransaction
        { get; set; }
        public System.Data.SQLite.SQLiteTransaction SQLiteTransaction
        { get; set; }
        /// <summary>
        /// 提交事务
        /// 提交事务后,将关闭链接对象
        /// </summary>
        public void Commit()
        {
            if (null != OracleTransaction)
            {
                OracleTransaction.Commit();
                OracleTransaction.Dispose();
                OracleConnection.Close();
                OracleConnection.Dispose();
            }
            else if (null != SqlTransaction)
            {
                SqlTransaction.Commit();
                SqlTransaction.Dispose();
                SqlConnection.Close();
                SqlConnection.Dispose();
            }
            else if (null != MySqlTransaction)
            {
                MySqlTransaction.Commit();
                MySqlTransaction.Dispose();
                MySqlConnection.Close();
                MySqlConnection.Dispose();
            }
            else if (null != SQLiteTransaction)
            {
                SQLiteTransaction.Commit();
                SQLiteTransaction.Dispose();
                SQLiteConnection.Close();
                SQLiteConnection.Dispose();
            }
        }
        /// <summary>
        /// 回滚事务
        /// 回滚事务后,将关闭链接对象
        /// </summary>
        public void Rollback()
        {
            if (null != OracleTransaction)
            {
                OracleTransaction.Rollback();
                OracleTransaction.Dispose();
                OracleConnection.Close();
                OracleConnection.Dispose();
            }
            else if (null != SqlTransaction)
            {
                SqlTransaction.Rollback();
                SqlTransaction.Dispose();
                SqlConnection.Close();
                SqlConnection.Dispose();
            }
            else if (null != MySqlTransaction)
            {
                MySqlTransaction.Rollback();
                MySqlTransaction.Dispose();
                MySqlConnection.Close();
                MySqlConnection.Dispose();
            }
            else if (null != SQLiteTransaction)
            {
                SQLiteTransaction.Rollback();
                SQLiteTransaction.Dispose();
                SQLiteConnection.Close();
                SQLiteConnection.Dispose();
            }
        }
    }
}

